/**
 * @file lv_port_indev_templ.c
 *
 */

/*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_indev.h"
#include "../../lvgl.h"
#include "user_cfg.h"
#include "gui_guider.h"
#include "key_process.h"
#include "key_scan.h"
#include "nrf_process.h"
#include "app.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

// static void touchpad_init(void);
// static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static bool touchpad_is_pressed(void);
// static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y);

//static void mouse_init(void);
//static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
//static bool mouse_is_pressed(void);
//static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y);

static void keypad_init(void);
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
extern uint32_t keypad_get_key(void);

//static void encoder_init(void);
//static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
//static void encoder_handler(void);

// static void button_init(void);
// static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
// static int8_t button_get_pressed_id(void);
// static bool button_is_pressed(uint8_t id);

/**********************
 *  STATIC VARIABLES
 **********************/
// lv_indev_t * indev_touchpad;
//lv_indev_t * indev_mouse;
lv_indev_t * indev_keypad;
//lv_indev_t * indev_encoder;
// lv_indev_t * indev_button;

//static int32_t encoder_diff;
//static lv_indev_state_t encoder_state;

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
    /**
     * Here you will find example implementation of input devices supported by LittelvGL:
     *  - Touchpad
     *  - Mouse (with cursor support)
     *  - Keypad (supports GUI usage only with key)
     *  - Encoder (supports GUI usage only with: left, right, push)
     *  - Button (external buttons to press points on the screen)
     *
     *  The `..._read()` function are only examples.
     *  You should shape them according to your hardware
     */

    static lv_indev_drv_t indev_drv;

    /*------------------
     * Touchpad
     * -----------------*/

    // /*Initialize your touchpad if you have*/
    // touchpad_init();

    // /*Register a touchpad input device*/
    // lv_indev_drv_init(&indev_drv);
    // indev_drv.type = LV_INDEV_TYPE_POINTER;
    // indev_drv.read_cb = touchpad_read;
    // indev_touchpad = lv_indev_drv_register(&indev_drv);

    /*------------------
     * Mouse
     * -----------------*/

//    /*Initialize your mouse if you have*/
//    mouse_init();

//    /*Register a mouse input device*/
//    lv_indev_drv_init(&indev_drv);
//    indev_drv.type = LV_INDEV_TYPE_POINTER;
//    indev_drv.read_cb = mouse_read;
//    indev_mouse = lv_indev_drv_register(&indev_drv);

//    /*Set cursor. For simplicity set a HOME symbol now.*/
//    lv_obj_t * mouse_cursor = lv_img_create(lv_scr_act());
//    lv_img_set_src(mouse_cursor, LV_SYMBOL_HOME);
//    lv_indev_set_cursor(indev_mouse, mouse_cursor);

    /*------------------
     * Keypad
     * -----------------*/

   /*Initialize your keypad or keyboard if you have*/
   keypad_init();

   /*Register a keypad input device*/

   lv_group_t * group = lv_group_create();
   lv_indev_drv_init(&indev_drv);
   indev_drv.type = LV_INDEV_TYPE_KEYPAD;
   indev_drv.read_cb = keypad_read;
    // indev_drv.read_cb = lv_custom_focus_keypad_read;
   indev_keypad = lv_indev_drv_register(&indev_drv);

   // 将焦点组绑定到输入设备
    lv_indev_set_group(indev_keypad, group);

   /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
    *add objects to the group with `lv_group_add_obj(group, obj)`
    *and assign this input device to group to navigate in it:
    *`lv_indev_set_group(indev_keypad, group);`*/

    /*------------------
     * Encoder
     * -----------------*/

//    /*Initialize your encoder if you have*/
//    encoder_init();

//    /*Register a encoder input device*/
//    lv_indev_drv_init(&indev_drv);
//    indev_drv.type = LV_INDEV_TYPE_ENCODER;
//    indev_drv.read_cb = encoder_read;
//    indev_encoder = lv_indev_drv_register(&indev_drv);

//    /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
//     *add objects to the group with `lv_group_add_obj(group, obj)`
//     *and assign this input device to group to navigate in it:
//     *`lv_indev_set_group(indev_encoder, group);`*/

    /*------------------
     * Button
     * -----------------*/

//    /*Initialize your button if you have*/
//    button_init();

//    /*Register a button input device*/
//    lv_indev_drv_init(&indev_drv);
//    indev_drv.type = LV_INDEV_TYPE_BUTTON;
//    indev_drv.read_cb = button_read;
//    indev_button = lv_indev_drv_register(&indev_drv);

//    /*按钮在屏幕上的点位*/
//    static const lv_point_t btn_points[] = {
//        {28, 40},   /*Button 0 -> x:10; y:10*/
//        {131, 40},  /*Button 1 -> x:40; y:100*/
//        {28, 164},
//        {131, 164},
//        {71, 217},   //页面1返回
//        {65, 255},   //页面2返回
//    };
//    lv_indev_set_button_points(indev_button, btn_points);
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*------------------
 * Touchpad
 * -----------------*/

// /*Initialize your touchpad*/
// static void touchpad_init(void)
// {
//     /*Your code comes here*/

// }

// /*Will be called by the library to read the touchpad*/
// static void touchpad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
// {
//     static lv_coord_t last_x = 0;
//     static lv_coord_t last_y = 0;

//     /*Save the pressed coordinates and the state*/
//     if(touchpad_is_pressed()) {
//         touchpad_get_xy(&last_x, &last_y);
//         data->state = LV_INDEV_STATE_PR;
//     }
//     else {
//         data->state = LV_INDEV_STATE_REL;
//     }

//     /*Set the last pressed coordinates*/
//     data->point.x = last_x;
//     data->point.y = last_y;
// }

// /*Return true is the touchpad is pressed*/
// static bool touchpad_is_pressed(void)
// {

// }

// /*Get the x and y coordinates if the touchpad is pressed*/
// static void touchpad_get_xy(lv_coord_t * x, lv_coord_t * y)
// {
//     /*Your code comes here*/

// }

/*------------------
 * Mouse
 * -----------------*/

///*Initialize your mouse*/
//static void mouse_init(void)
//{
//    /*Your code comes here*/
//}

///*Will be called by the library to read the mouse*/
//static void mouse_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
//{
//    /*Get the current x and y coordinates*/
//    mouse_get_xy(&data->point.x, &data->point.y);

//    /*Get whether the mouse button is pressed or released*/
//    if(mouse_is_pressed()) {
//        data->state = LV_INDEV_STATE_PR;
//    }
//    else {
//        data->state = LV_INDEV_STATE_REL;
//    }
//}

///*Return true is the mouse button is pressed*/
//static bool mouse_is_pressed(void)
//{
//    /*Your code comes here*/

//    return false;
//}

///*Get the x and y coordinates if the mouse is pressed*/
//static void mouse_get_xy(lv_coord_t * x, lv_coord_t * y)
//{
//    /*Your code comes here*/

//    (*x) = 0;
//    (*y) = 0;
//}

/*------------------
 * Keypad
 * -----------------*/

/*Initialize your keypad*/
static void keypad_init(void)
{
   /*Your code comes here*/
   Key_Init();
}
// lv_ui guider_ui;

/*Will be called by the library to read the mouse*/

extern void handle_physical_back_button(void);
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static uint32_t last_key = 0, hold_key = 0;
    uint32_t cur_key = keypad_get_key();


    if (cur_key != 0) {
        data->state = LV_INDEV_STATE_PR;
        last_key = cur_key;
        TimeOut_Record(&app_para.tout.sleep_tout, SLEEP_TIMEOUT);


    } else {
        hold_key = last_key;                    //按键保持时只触发一次
        data->state = LV_INDEV_STATE_REL;
        nrf_para.long_press_pair = false;
        if (nrf_para.nrf.send_cmd) {
            nrf_para.nrf.stop_send_flag = true;
            nrf_para.nrf.start_send_flag = false;
            TimeOut_Record(&nrf_para.tout.release_tout, 300);
            nrf_para.nrf.send_cmd = 0;
        }
        if(last_key == 6) {                     //松手以后返回
            handle_physical_back_button();
        }

        if (lv_scr_act() == guider_ui.motion_1) {
            if(last_key == 7) {
                scroll_motion1_icon_select(0, &guider_ui);
            } else if (last_key == 2) {
                scroll_motion1_icon_select(1, &guider_ui);
            } else if (last_key == 1) {
                Sw_icon_select(0, &guider_ui);
            } else if (last_key == 4) {
                Sw_icon_select(1, &guider_ui);
            }
        } else if (lv_scr_act() == guider_ui.motion_2) {
            if(last_key == 7) {
                scroll_motion2_icon_select(0, &guider_ui);
            } else if (last_key == 2) {
                scroll_motion2_icon_select(1, &guider_ui);
            }
        }  else if (lv_scr_act() == guider_ui.main){
            nrf_para.nrf.stop_send_flag = true;
            nrf_para.nrf.start_send_flag = false;
            TimeOut_Record(&nrf_para.tout.release_tout, 100);
            nrf_para.nrf.send_cmd = 0;
        }
        // else if (lv_scr_act() == guider_ui.light_1) {
        //     if(last_key == 7) {
        //         scroll_light_1_icon_select(0, &guider_ui);
        //     } else if (last_key == 2) {
        //         scroll_light_1_icon_select(1, &guider_ui);
        //     }
        // }
        //  else if (lv_scr_act() == guider_ui.massage_intensity) {
        //     if(last_key == 7) {
        //         scroll_light_1_icon_select(0, &guider_ui);
        //     } else if (last_key == 2) {
        //         scroll_light_1_icon_select(1, &guider_ui);
        //     }
        // }
        last_key = cur_key;
    }



    // 当前处于 screen_1 页面
    if (lv_scr_act() == guider_ui.motion_1 || lv_scr_act() == guider_ui.motion_2\
    || lv_scr_act() == guider_ui.light_1) {
        switch(last_key) {
            case 5:
                data->key = LV_KEY_ENTER;
                break;

            case 6:
                data->key = LV_KEY_ESC;
                break;

            case 8:
                nrf_para.long_press_pair = true;
                break;

            case 9 :
                data->key = 0;
                nrf_para.nrf.start_send_flag = true;
                nrf_para.nrf.send_cmd = PUSHROD_RESET;
                break;
            default:
                data->key = 0;
                break;
        }
    } else if (lv_scr_act() == guider_ui.massage_mode || lv_scr_act() == guider_ui.massage_time\
    || lv_scr_act() == guider_ui.massage_intensity) {
        if (hold_key != last_key) {
            switch(last_key) {
                case 1:
                    data->key = LV_KEY_UP;
                    hold_key = last_key;
                    nrf_para.nrf.start_send_flag = true;
                    if (lv_scr_act() == guider_ui.massage_intensity) {
                        if (guider_ui.msg_intensity_pos.foot)
                            nrf_para.nrf.send_cmd = MASSAGE_FOOT_INS_DOWN_CMD;
                        else
                            nrf_para.nrf.send_cmd = MASSAGE_HEAD_INS_DOWN_CMD;
                    } else if (lv_scr_act() == guider_ui.massage_mode) {
                        nrf_para.nrf.send_cmd = MASSAGE_LAST_MODE_CMD;
                    } else if (lv_scr_act() == guider_ui.massage_time) {
                        nrf_para.nrf.send_cmd = MASSAGE_LAST_TIMER_CMD;
                    }
                    break;

                case 4:
                    data->key = LV_KEY_DOWN;
                    hold_key = last_key;
                    nrf_para.nrf.start_send_flag = true;
                    if (lv_scr_act() == guider_ui.massage_intensity) {
                        if (guider_ui.msg_intensity_pos.foot)
                            nrf_para.nrf.send_cmd = MASSAGE_FOOT_INS_UP_CMD;
                        else
                            nrf_para.nrf.send_cmd = MASSAGE_HEAD_INS_UP_CMD;
                    } else if (lv_scr_act() == guider_ui.massage_mode) {
                        nrf_para.nrf.send_cmd = MASSAGE_NEXT_MODE_CMD;
                    } else if (lv_scr_act() == guider_ui.massage_time) {
                        nrf_para.nrf.send_cmd = MASSAGE_NEXT_TIMER_CMD;
                    }
                    break;

                case 7: // 左
                    if (lv_scr_act() == guider_ui.massage_intensity) {
                        msg_pos_sw(&guider_ui);
                        hold_key = last_key;
                    }
                    break;

                case 2: // 右
                    if (lv_scr_act() == guider_ui.massage_intensity) {
                        msg_pos_sw(&guider_ui);
                        hold_key = last_key;
                    }
                    break;

                case 5:
                    data->key = LV_KEY_ENTER;
                    break;

                case 8:
                    nrf_para.long_press_pair = true;
                    break;

                case 9 :
                    data->key = 0;
                    nrf_para.nrf.start_send_flag = true;
                    nrf_para.nrf.send_cmd = PUSHROD_RESET;
                    break;

                default:
                    data->key = 0;
                    break;

            }
        } else {
            data->key = 0;
        }
    } else {
        switch(last_key) {
            case 1: // 上
            case 7: // 左
                data->key = LV_KEY_PREV;
                break;

            case 2: // 右
            case 4: // 下
                data->key = LV_KEY_NEXT;
                break;

            case 5:
                data->key = LV_KEY_ENTER;
                break;

            case 6:
                data->key = LV_KEY_ESC;
                break;

            case 8:
                nrf_para.long_press_pair = true;
                break;

            case 9 :
                data->key = 0;
                nrf_para.nrf.start_send_flag = true;
                nrf_para.nrf.send_cmd = PUSHROD_RESET;
                break;
            default:
                data->key = 0;
                break;
        }
    }
}



/*Get the currently being pressed key.  0 if no key is pressed*/

#define KEY_K1_PORT             GPIOF
#define KEY_K1_PIN              GPIO_Pin_7
#define KEY_K2_PORT             GPIOA
#define KEY_K2_PIN              GPIO_Pin_10
#define KEY_K3_PORT             GPIOA
#define KEY_K3_PIN              GPIO_Pin_9
#define KEY_K4_PORT             GPIOA
#define KEY_K4_PIN              GPIO_Pin_11
#define KEY_K5_PORT             GPIOF
#define KEY_K5_PIN              GPIO_Pin_6
#define KEY_K6_PORT             GPIOA
#define KEY_K6_PIN              GPIO_Pin_12
#define KEY_K7_PORT             GPIOB
#define KEY_K7_PIN              GPIO_Pin_8


uint32_t keypad_get_key(void)
{
    uint8_t cnt = 0, value = 0, last_val = 0, test_val = 0;
    if (!GPIO_ReadInputDataBit(KEY_K1_PORT, KEY_K1_PIN)) {
        value = 1, cnt++;
    }
     if (!GPIO_ReadInputDataBit(KEY_K2_PORT, KEY_K2_PIN)) {
        value = 2, cnt++;
        test_val++;
    }
    if (!GPIO_ReadInputDataBit(KEY_K3_PORT, KEY_K3_PIN)) {
        value = 3, cnt++;
    }
    if (!GPIO_ReadInputDataBit(KEY_K4_PORT, KEY_K4_PIN)) {
        value = 4, cnt++;
    }
    if (!GPIO_ReadInputDataBit(KEY_K5_PORT, KEY_K5_PIN)) {
        value = 5, cnt++;
        last_val++;
    }
    if (!GPIO_ReadInputDataBit(KEY_K6_PORT, KEY_K6_PIN)) {
        value = 6, cnt++;
        last_val++;
    }
    if (!GPIO_ReadInputDataBit(KEY_K7_PORT, KEY_K7_PIN)) {
        value = 7, cnt++;
        test_val++;
    }
    if (cnt > 1) {
        if (last_val > 1) {
            value = 8;                                  //进入配对
        }  else if (test_val > 1) {
            value = 9;                                  //下发
        } else {
            value = 0;
        }
    }

   return value;
}

/*------------------
 * Encoder
 * -----------------*/

///*Initialize your keypad*/
//static void encoder_init(void)
//{
//    /*Your code comes here*/
//}

///*Will be called by the library to read the encoder*/
//static void encoder_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
//{

//    data->enc_diff = encoder_diff;
//    data->state = encoder_state;
//}

///*Call this function in an interrupt to process encoder events (turn, press)*/
//static void encoder_handler(void)
//{
//    /*Your code comes here*/

//    encoder_diff += 0;
//    encoder_state = LV_INDEV_STATE_REL;
//}

/*------------------
 * Button
 * -----------------*/

// /*Initialize your buttons*/
// static void button_init(void)
// {
//    /*Your code comes here*/

//     Key_Msg_Init();
// }

// /*Will be called by the library to read the button*/
// static void button_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
// {
//    static uint8_t last_btn = 0;

//    /*Get the pressed button's ID*/
//    int8_t btn_act = button_get_pressed_id();

//    if(btn_act >= 0) {
//        data->state = LV_INDEV_STATE_PR;
//        last_btn = btn_act;
//    }
//    else {
//        data->state = LV_INDEV_STATE_REL;
//    }

//    /*Save the last pressed button's ID*/
//    data->btn_id = last_btn;
// }

// /*Get ID  (0, 1, 2 ..) of the pressed button*/
// static int8_t button_get_pressed_id(void)
// {
// //    int8_t i;

//    /*Check to buttons see which is being pressed (assume there are 2 buttons)*/
// //    for(i = 0; i < 4; i++) {
// //        /*Return the pressed button's ID*/
// //        if(button_is_pressed(i)) {
// //            return i;
// //        }
// //    }
//     // i = Get_KeyIndex_Port();
// //    return Get_KeyIndex_Port();
//     return Key_Event_Handle(Key_Scan());

//    /*No button pressed*/
// //    return -1;
// }

// /*Test if `id` button is pressed or not*/
// static bool button_is_pressed(uint8_t id)
// {

//     // /** add your code below */

// //     if (id == 0) {
// //         return !GPIO_ReadInputDataBit(KEY_K1_PORT, KEY_K1_PIN);
// //     } else if (id == 1) {
// //         return !GPIO_ReadInputDataBit(KEY_K2_PORT, KEY_K2_PIN);
// //     } else if (id == 2) {
// //         return !GPIO_ReadInputDataBit(KEY_K3_PORT, KEY_K3_PIN);
// //     } else if (id == 3) {
// //         return !GPIO_ReadInputDataBit(KEY_K4_PORT, KEY_K4_PIN);
// //     }

// //    /*Your code comes here*/
// //    return false;
// }

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif
